package com.zhouyangyang.system.controller;

import com.zhouyangyang.entity.Result;
import com.zhouyangyang.entity.StatusCode;
import com.zhouyangyang.system.pojo.Admin;
import com.zhouyangyang.system.service.AdminService;
import com.zhouyangyang.system.util.JwtUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @PackageName: com.zhouyangyang.system.controller
 * @ClassName: AdminController
 * @Author: zhouyangyang
 * @Date: 2020/8/9 21:45
 * @Description:
 */
@RequestMapping("/admin")
@RestController
@RequiredArgsConstructor
public class AdminController {
    private final AdminService adminService;
    /**
     * 登录
     */
    @PostMapping("/login")
    public Result login(@RequestBody Admin admin){
        Boolean login = adminService.login(admin);
        if (login){ //如果验证成功
            Map<String,String> map = new HashMap<>();
            map.put("username",admin.getLoginName());
            String token = JwtUtil.creatJWT(UUID.randomUUID().toString(), admin.getLoginName(), null);
            map.put("Token",token);
            return new Result(true,StatusCode.OK,"登陆成功",map);
        }
        return new Result(false, StatusCode.LOGINERROR,"用户名或密码错误");
    }
}
